package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.service.MemberService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author hql
 * @date 2019-12-11 21:03
 */
@RestController
@RequestMapping("/member")
public class MemberController {
    @Reference
    private MemberService memberService;

    @RequestMapping("/getMemberReport")
    public Result getMemberReport(){
        try {
            //获取calendar对象
            Calendar calendar = Calendar.getInstance();
            //获取12个月之前的日期
            calendar.add(Calendar.MONTH,-12);

            List<String> list = new ArrayList<>();
            for (int i = 0; i < 12; i++) {
                calendar.add(Calendar.MONTH,1);
                list.add(new SimpleDateFormat("yyyy.MM").format(calendar.getTime()));
            }
            Map<String,Object> map = new HashMap<>();
            map.put("months",list);

            List<Integer> list2 =  memberService.getMemberReport(list);
            map.put("memberCount",list2);
            return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
        }

    }

}
